﻿cmake_minimum_required(VERSION 3.15)
project(BridgeAnalysis LANGUAGES CXX)

# 定义库的版本号（根据实际需求修改）
set(BRIDGE_ANALYSIS_VERSION_MAJOR 0)    # 主版本号（不兼容变更）
set(BRIDGE_ANALYSIS_VERSION_MINOR 0)    # 次版本号（兼容新增功能）
set(BRIDGE_ANALYSIS_VERSION_PATCH 1)    # 修订号（兼容修复）
set(BRIDGE_ANALYSIS_VERSION "${BRIDGE_ANALYSIS_VERSION_MAJOR}.${BRIDGE_ANALYSIS_VERSION_MINOR}.${BRIDGE_ANALYSIS_VERSION_PATCH}")
message(STATUS "BridgeAnalysis库版本: ${BRIDGE_ANALYSIS_VERSION}")

# 设置C++标准
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 设置C标准
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)

message(STATUS "CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
cmake_path(GET CMAKE_SOURCE_DIR PARENT_PATH SOLUTION_DIR)
message(STATUS "SOLUTION_DIR: ${SOLUTION_DIR}")

# 配置编译选项
if(MSVC)
    add_compile_options(
        /W3
        /sdl
        /permissive-
        /wd4200 /wd4244  # 禁用特定警告
    )
    
    # 根据配置类型设置不同的编译选项
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        add_compile_definitions(
            BOOST_ALL_NO_LIB
            BOOST_ALL_DYN_LINK
            BOOST_STACKTRACE_LINK
            BOOST_STACKTRACE_USE_ADDR2LINE
            BOOST_STACKTRACE_USE_WINDBG
            _USRDLL
            _DEBUG
            _CONSOLE
        )
    else()
        add_compile_definitions(
            BOOST_ALL_NO_LIB
            BOOST_ALL_DYN_LINK
            BOOST_STACKTRACE_LINK
            BOOST_STACKTRACE_USE_ADDR2LINE
            BOOST_STACKTRACE_USE_WINDBG
            _USRDLL
            NDEBUG
            _CONSOLE
        )
    endif()

    # 64位架构额外优化（可选）
    if(ARCH STREQUAL "x64")
        add_compile_options(/arch:AVX2)  # 针对64位CPU优化
    endif()
else()
    add_compile_options(
        -Wall
        -Wextra
        -Wno-pedantic        
        -Wno-unknown-pragmas
        -Wno-unused-parameter
        -Wno-missing-field-initializers
        -Wno-unused-variable
        -fPIC  # 生成位置无关代码，用于动态库
    )
    # 针对 Clang 的额外选项
    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        add_compile_options(-stdlib=libc++)
    endif()
     # 根据配置类型设置不同的编译选项
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        add_compile_definitions(
            BOOST_ALL_NO_LIB
            BOOST_ALL_DYN_LINK
            _USRDLL
            _DEBUG
            _CONSOLE
        )
    else()
        add_compile_definitions(
            BOOST_ALL_NO_LIB
            BOOST_ALL_DYN_LINK
            _USRDLL
            NDEBUG
            _CONSOLE
        )
    endif()
endif()

# 设置包含目录
set(INCLUDE_DIRS
    /usr/include
    ${SOLUTION_DIR}/libs/include
    ${SOLUTION_DIR}/libs/include/jdbc
    ${SOLUTION_DIR}
    ${CMAKE_SOURCE_DIR}
)

include_directories(${INCLUDE_DIRS})

# 设置源文件
set(SOURCES
    ${CMAKE_SOURCE_DIR}/BridgeAnalysis.cpp
    ${CMAKE_SOURCE_DIR}/OrgFile.cpp
    ${CMAKE_SOURCE_DIR}/MethodFile.cpp
    ${CMAKE_SOURCE_DIR}/AnalysisDLL.cpp
    ${CMAKE_SOURCE_DIR}/utils.cpp
    ${CMAKE_SOURCE_DIR}/BridgeDLL.cpp
    ${CMAKE_SOURCE_DIR}/Sampling.cpp
    ${CMAKE_SOURCE_DIR}/uuid_generator.cpp
    ${CMAKE_SOURCE_DIR}/pch.cpp
)

# 设置头文件
set(HEADERS
    ${CMAKE_SOURCE_DIR}/BridgeAnalysis.h
    ${CMAKE_SOURCE_DIR}/OrgFile.h
    ${CMAKE_SOURCE_DIR}/MethodFile.h
    ${CMAKE_SOURCE_DIR}/AnalysisDLL.h
    ${CMAKE_SOURCE_DIR}/utils.h
    ${CMAKE_SOURCE_DIR}/BridgeDLL.h
    ${CMAKE_SOURCE_DIR}/defines.h
    ${CMAKE_SOURCE_DIR}/errors.h
    ${CMAKE_SOURCE_DIR}/Sampling.h
    ${CMAKE_SOURCE_DIR}/uuid_generator.h
)



# === 修复：正确设置链接路径和库 ===
# 设置目标架构（默认x64，可通过-DARCH=Win32覆盖）
set(ARCH "x64" CACHE STRING "目标架构：Win32或x64")

# 设置输出目录
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)

# 设置链接库
if(WIN32)
    # 基于ARCH变量设置库路径（删除手动覆盖的部分）
    set(LIB_DIR ${SOLUTION_DIR}/libs/lib/${ARCH})
    
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(LIB_DIR ${LIB_DIR}/Debug)
    else()
        set(LIB_DIR ${LIB_DIR}/Release)
    endif()    

    message(STATUS "LIB_DIR: ${LIB_DIR}")
    link_directories(${LIB_DIR})    

    # 创建动态库（对应vcxproj中的DynamicLibrary）
    add_library(BridgeAnalysis SHARED
        ${SOURCES}
        ${HEADERS}
    )
else()
    set(LIB_DIR ${SOLUTION_DIR}/libs/lib/linux)

    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(LIB_DIR ${LIB_DIR}/Debug)
    else()
        set(LIB_DIR ${LIB_DIR}/Release)
    endif()

    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(HIREDIS_LIB "hiredisd")
    else()
        set(HIREDIS_LIB "hiredis")
    endif()
    set(BOOST_LIBS
        boost_filesystem
        boost_log
        boost_log_setup
        boost_thread
        boost_atomic
        boost_json
        boost_locale
        # boost_system
    )
    set(OTHER_LIBS
        mysqlcppconn
        redis++
        ${HIREDIS_LIB}
        pthread  # Linux 线程库
    )

    message(STATUS "LIB_DIR: ${LIB_DIR}")
    link_directories(${LIB_DIR})    

    # 创建动态库（对应vcxproj中的DynamicLibrary）
    add_library(BridgeAnalysis SHARED
        ${SOURCES}
        ${HEADERS}
    )
    target_link_libraries(BridgeAnalysis
        ${BOOST_LIBS}
        ${OTHER_LIBS}
    )
    set_target_properties(BridgeAnalysis PROPERTIES
        # 完整版本号：生成libBridgeAnalysis.so.x.y.z
        VERSION ${BRIDGE_ANALYSIS_VERSION}
        # 主版本号：生成libBridgeAnalysis.so.x（链接时使用的主版本）
        SOVERSION ${BRIDGE_ANALYSIS_VERSION_MAJOR}
        # 确保库输出到指定目录（与你的现有配置匹配）
        LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
    )
endif()

set(CMAKE_PCH_ENABLED TRUE)
target_precompile_headers(BridgeAnalysis PRIVATE 
    ${SOLUTION_DIR}/BridgeAnalysis/pch.h
)

if(MSVC)
    set(INSTALL_DIR "${SOLUTION_DIR}/${ARCH}/${CMAKE_BUILD_TYPE}")
else()
    set(INSTALL_DIR "${SOLUTION_DIR}/Linux/${CMAKE_BUILD_TYPE}")
endif()
message(STATUS "安装路径: ${INSTALL_DIR}")

# 确保安装路径存在（递归创建）
file(MAKE_DIRECTORY ${INSTALL_DIR})

# 安装配置
install(TARGETS BridgeAnalysis
    RUNTIME DESTINATION "${INSTALL_DIR}"
    LIBRARY DESTINATION "${INSTALL_DIR}"
    ARCHIVE DESTINATION "${INSTALL_DIR}"
)

# 设置Windows目标平台版本
if(WIN32)
    set(CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION "10.0")
    set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

# Windows下32位构建
# "x86 Native Tools Command Prompt for VS 2022"下，下一行命令可省略，已经执行过了。
# "D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32.bat"
# ############# 构建Windows Win32 Release版本 ######################
# cd D:\Work\Bridge6\BridgeAnalysis
# mkdir build\win32_release
# cd build/win32_release
# cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=Win32 
# ninja -v -j8
# ninja install
# ############# 构建Windows Win32 Debug版本 ######################
# cd D:\Work\Bridge6\BridgeAnalysis
# mkdir build\win32_debug
# cd build/win32_debug
# cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=Win32
# ninja -v -j8
# ninja install

# Windows下64位构建
# "x64 Native Tools Command Prompt for VS 2022"下，下一行命令可省略，已经执行过了。
# "D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars32.bat"
# ############# 构建Windows x64 Release版本 ######################
# cd D:\Work\Bridge6\BridgeAnalysis
# mkdir build\x64_release
# cd build/x64_release
# cmake ..\.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=x64 
# ninja -v -j8
# ninja install
# ############# 构建Windows x64 Debug版本 ######################
# cd D:\Work\Bridge6\BridgeAnalysis
# mkdir build\x64_debug
# cd build/x64_debug
# cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=x64
# ninja -v -j8
# ninja install

# Linux下64位构建
# ############# 构建Linux x64 Release版本 ######################
# cd /root/work/bridge6/BridgeAnalysis
# mkdir -p build/linux_x64_release
# cd build/linux_x64_release
# cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
# ninja -v -j$(nproc)
# ninja install
# ############# 构建Linux x64 Debug版本 ######################
# cd /root/work/bridge6/BridgeAnalysis
# mkdir -p build/linux_x64_debug
# cd build/linux_x64_debug
# cmake ../.. -G "Ninja" -DCMAKE_BUILD_TYPE=Debug -DARCH=aarch64 -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
# ninja -v -j$(nproc)
# ninja install
